home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Languages / Masm V6.11 / SAMPLES / DOSDEV / ATOMS.TX$ / ATOMS
Encoding:
Text File  |  1992-11-12  |  7.6 KB  |  129 lines

  1. To create the appropriate files for this demo, do the following:
  2.     To create ATOMS.SYS:         do    ML -FeATOMS.SYS ATOMS.ASM
  3.     To create ATOMTEST.EXE:     do    ML ATOMTEST.ASM
  4.     To create CTEST.EXE:        do    CL CTEST.C
  5.  
  6. How does a device driver work? A BRIEF summary for the non-experts:
  7.     Devices are accessed through interrupt 21h functions. They can be 
  8. character or block devices. Requests from a program are converted into 
  9. requests to the driver by DOS. Basically, DOS takes the contents of DS, 
  10. DX, BX, CX, and AX, converts them and stores them in the request header 
  11. structure. It first calls the Driver's Strategy routine, which should 
  12. store the pointers to the structure so they can be used later. Then, DOS 
  13. calls the actual Interrupt routine, which should handle the request. The 
  14. device gets its data and outputs its results through the request header 
  15. structure.
  16.          To make a request to the device, you need a handle to it. To get 
  17. the handle, you use the open device function. Then, to make a call you 
  18. put the handle in BX, and whatever other data, if applicable. The buffer 
  19. segment goes in DS, the buffer offset in DX, the byte count in CX, the 
  20. function in AX. Then do an INT 21h and you're set.
  21.     Every driver is initialized only once, and at that point it has to 
  22. tell DOS how much memory it uses (its break address). At this time it 
  23. also gets pointers to the CONFIG.SYS = line that loaded the driver. 
  24. Since the initialization function is used only once, its memory space 
  25. can be deallocated after its use.
  26.     Device drivers are usually binary images, the same format that 
  27. .COM files use. Therefore, you either have to use a 'TINY' model 
  28. directive or use a utility such as EXE2BIN to convert you program. 
  29. Although .EXE files can also be drivers, their use is not recommended. 
  30. Driver files have to start at 0000h in memory, therefore the .ORG 0000h 
  31. directive. They also can't have stacks. In linking you will get an 
  32. error saying that the tiny program doesn't start at 0100h: this is 
  33. normal. Only .COM files need to start at that address. The first thing 
  34. at address 0000h has to be the driver header. See the source code for 
  35. details on it.
  36.  
  37. How the ATOMS device driver works:
  38.     The atoms driver, referenced from DOS by ATMS, is a simple driver 
  39. that possesses some of the functionality of environment variables. It 
  40. allows users to set variables in the driver's memory space, but the 
  41. difference is  that a variable set in the driver can be accessed by any 
  42. program in any other environment. They can run at different times, in 
  43. different Windows DOS-boxes, etc. The driver code takes less than 600 
  44. bytes of memory, plus whatever buffer you specify.
  45.     The operations the driver supports are: the DOS Initialization 
  46. (required for all drivers), Output Status (i.e. is there still memory 
  47. available), Device Open, Device Close, and of course Read (transfer from 
  48. the atom selected in write to a buffer) and Write. Write is the biggest 
  49. chunk of code and has the largest functionality: If the input string is 
  50. 'variable', then it will search for the variable in memory and set the 
  51. output buffer to point to it. If the input is 'variable=', then the 
  52. variable is deleted from memory. If the input is 'variable=value' then 
  53. variable is set into the memory. A variable name can be ANY character 
  54. except '=', linefeed, carriage return, or '\0' (ASCII 0).
  55.     Some details on how the write function works: when it's doing an 
  56. insert, if the driver finds the variable in memory then it first checks 
  57. to see if there's enough space for the new size of the variable, and 
  58. then inserts the new atom. Also, a potential for 'race' problems exists 
  59. if programs are running concurrently: if a program wants to do a search 
  60. and it writes the request to the driver, there's a chance that another 
  61. program will do a read after before the original program can do it, and 
  62. get that program's search result.
  63.     The driver can be loaded with the DEVICE= or DEVICEHIGH= 
  64. directives in the CONFIG.SYS file. To load the driver, insert a 
  65. 'DEVICE=[path]ATOMS.SYS' line in the file. To change the driver memory 
  66. from the default 1K buffer, follow the device name with a blank space 
  67. and a decimal number specifying the buffer size in bytes, for example, 
  68. 'DEVICE=C:\DOS\ATOMS.SYS  4000'. Non-digit characters will be ignored. 
  69.     Raw vs. Cooked mode: when loaded, drivers are initially in cooked 
  70. mode. This means that DOS will search through input/output of the device 
  71. for special characters like a CTRL-C, etc. Cooked mode also means that 
  72. DOS will make the driver read or write one character at a time: if 
  73. there's a request to read 20 characters, DOS would make 20 one-character 
  74. requests. The Atoms driver simply cannot function in cooked mode, and 
  75. the first thing after you open the device should be to put the device 
  76. into raw mode (no translation) see the sample code for details on how to 
  77. do this. In C, you can do this by linking BINMODE.OBJ with the object 
  78. file.
  79.     Atoms is a sample driver, and there's lots of room to improve the 
  80. driver: assignments cannot reference other atoms in memory, there's no 
  81. way to output the whole memory buffer, and insert are somewhat 
  82. inefficient in that space for the new value could be made at the 
  83. previous location.
  84.     One last thing: remember you can't have a file with the same name 
  85. as the device driver (i.e. you can't have an atms.sys file, or an atms 
  86. directory).
  87.  
  88. For a complete discussion of MS-DOS device drivers, check out books like 
  89. The MS-DOS Encyclopedia (c) 1988 Microsoft Press, or the Microsoft MS-
  90. DOS Programmer's Reference (c) 1991.                
  91.  
  92. The Device Driver's Code
  93.     The code for the device driver can be cleanly separated into two 
  94. areas: Code absolutely required for device drivers, and code that is 
  95. required for the implementation of this device driver.
  96.     The Device Header, Strategy Routine, and Interrupt Routine, are 
  97. required for all drivers. The header specifies the link to the next 
  98. device (the -1 is replaced by DOS). The Attribute word identifies us as 
  99. a character device that supports Open and Close. The Strat and Intr 
  100. NPTRs tell DOS the entry points when calling the driver.
  101.     The Strategy routine's only function should be to store the 
  102. pointer to the request header , which DOS puts in ES:BX before calling 
  103. it.
  104.     The Interrupt routine is a bit more complicated: It should make 
  105. sure registers are preserved, call the appropriate code routine, and set 
  106. the status word. The typical way to handle the different request is to 
  107. have a Dispatch Table.
  108.     The rest of the code is called by the Interrupt routine.
  109.     The Init procedure shows the sign-on message, calls on Scan to 
  110. evaluate the command line, and sets the request header with the break 
  111. address for the pointer.
  112.     The Scan procedure 'scans' the command line for the number of 
  113. bytes to allocate.
  114.     The Read procedure copies from what pAtomVal is pointing to the 
  115. data buffer specified in the request header.
  116.     The Output Status procedure checks to see if there's at least one 
  117. more space available in the driver memory.
  118.     The Device Close, Open and Input Status routines simply set the 
  119. status word to OK.
  120.     The Write routine's algorithm: get the length of the source 
  121. buffer, find out what operation to perform, try to find a match within 
  122. the memory. If doing an insert, check to see if a delete is needed, then 
  123. insert the atom. Then, if a match was found, set pAtomVal or delete the 
  124. atom from memory, as necessary. 
  125.     The Delete routine gets an initial address and a byte count, and 
  126. shifts elements back according to the count.
  127.     The st_len routine puts the length of a string into ax.
  128.  
  129.